<?php

/**
 * Class used to comunicate with database, created on base of PDO
 *
 * @author shenlon
 */
class DBConnection {

    /**
     * Database connection data
     */
    private static $dbData = array(
        'engine' => 'mysql',
        'db' => 'sklep',
        'host' => 'localhost',
        'username' => 'sklep',
        'password' => 'sklep',
    );

    /**
     *
     * @var PDO Database connection
     */
    private static $pdo;

    /**
     * Constructor allowing to set non-default connection dagta
     * @param Array $data
     * @throws Exception
     */
    public function __construct($data = null) {
        if ($data != null) {
            if (!is_array($data)) {
                throw new Exception("Connection data should be array");
            }
            foreach ($data as $key => $value) {
                self::$dbData[$key] = $value;
            }
        }
        if (($data != null && $this->data != $data) || !isset(self::$pdo))
            $this->connect();
    }

    /**
     * Function creating connection string used to connect with database
     * @return string
     */
    private function createConnectionString() {
        $connectionString = self::$dbData['engine'] . ":host=" . self::$dbData['host'] . ";dbname=" . self::$dbData['db'];
        return $connectionString;
    }

    /**
     * Connects to database
     * @return boolean
     */
    private function connect() {
        $connectionString = $this->createConnectionString();
        try {
            self::$pdo = new PDO($connectionString, self::$dbData['username'], self::$dbData['password'],
                            array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
        } catch (PDOException $e) {
            echo "Could not connect to database. Exception: $e.";
            echo "Please revise the connection data:<br/>";
            foreach ($this->dbData as $key => $value) {
                echo "<li>$key: $value</li>";
            }
        }
        return true;
    }

    /**
     * executes query and returns fetched rows
     * @param String $query
     * @param Array $data
     * @return Array 
     */
    public function executeQuery($query, $data = array()) {
        $statement = self::$pdo->prepare($query);
        $statement->execute($data);
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        $statement->closeCursor();
        return $result;
    }

    /**
     * Executes specified update query - delete/insert/update
     * @param string $query
     * @param Array $data
     * @param boolean $getLastInsertID
     * @return int|Array 
     */
    public function executeUpdate($query, $data = array(), $getLastInsertID = false) {
        try {
            $preparedStatement = self::$pdo->prepare($query);
            if ($getLastInsertID) {
                $preparedStatement->execute($data);
                return self::$pdo->lastInsertId();
            }
            else
                return $preparedStatement->execute($data);
        } catch (Exception $e) {
            echo $e;
        }
    }

}

?>
